package com.yrd.fssync.core;

import java.io.IOException;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

public class SyncEngineMap {
	Context context;
	Map<String, SyncEngine> map;
	
	public SyncEngineMap(Context context) {
		this.context = context;
		map = new HashMap<String, SyncEngine>();
	}
	
	
	
	public Map<String, SyncEngine> getMap() {
		return map;
	}



	public void add(SyncEngine syncEngine){
		map.put(syncEngine.id, syncEngine);
	}
	
	public void post(SyncRequest request,String target){
		if(StringUtils.isEmpty(target)){
			for(SyncEngine engine : map.values()){
				engine.post(request);
			}
		}else{
			map.get(target).post(request);
		}
		
	}
	public void post(SyncRequest request){
		post(request,"");
		
	}
	
	public void start() throws SocketException, IOException{
		for(SyncEngine engine : map.values()){
			engine.start();;
		}
	}
	
	public void stop(){
		for(SyncEngine engine : map.values()){
			engine.stop();
		}
	}
}
